// hyb_yl/zhuanjiasubpages/pages/zhifuend/zhifuend.js
var app = getApp()
var interval;
var time1 = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pjxing: 0,
    txt: "",
    pingjia: false, //评价弹框
    pjvalue: "",
    sendlist: ["问诊异常通知", "问诊提醒", "医护服务动态提醒"],
    applyFor: false,
    popshow: false,
    shouwtab: false,
    upload_picture_list: [],
    typedate: 0,
    msgmb: 'yes',
    imgs: [
      "https://img1.dxycdn.com/2020/0308/949/3400928984622785985-22.jpg",
      "https://img1.dxycdn.com/2020/0308/949/3400928984622785985-22.jpg",
      "https://img1.dxycdn.com/2020/0308/949/3400928984622785985-22.jpg",
      "https://img1.dxycdn.com/2020/0308/949/3400928984622785985-22.jpg",
    ]
  },
  //关闭评价
  closepj() {
    this.setData({
      pingjia: false
    })
  },
  //关闭追问
  closezhuiwen() {
    this.setData({
      shouwtab: false
    })
  },
  // 订单完成
  succ:function(e){
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content:'确认订单已完成吗？',
      success:function(con)
      {
        if(con.confirm)
        {
          app.util.request({
            'url':"entry/wxapp/green.over_order",
            data:{
              id:id,
            },
            success:function(res){
              wx.showToast({
                title: '操作成功',
              })
              setTimeout(() => {
                wx.navigateBack({
                  complete: (res) => {},
                })
              }, 1000);
            }
          })
        }
      }

    })
  },
  tabinput(e) {
    this.setData({
      shouwtab: !this.data.shouwtab
    })
  },
  xing(e) {
    let num = e.currentTarget.dataset.dex
    this.setData({
      pjxing: num * 1 + 1
    })
  },
  addxing(e) {
    let num = e.currentTarget.dataset.dex,
      fen = this.data.pjxing
    let pf = num * 1 + fen * 1 + 1
    this.setData({
      pjxing: pf
    })
  },
  pjtextarea(e) {
    this.setData({
      pjvalue: e.detail.value
    })
  },
  //提交
  submitpj() {
    let num = this.data.pjxing,
      val = this.data.pjvalue
    this.setData({
      pingjia: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showNavigationBarLoading();
    var that = this
    var zid = options.zid
    var back_orser = options.back_orser
    var bgc = wx.getStorageSync('color')
    var key_words = options.key_words
    var j_id = options.j_id
    var ifpay = options.ifpay
    var money = options.money
    var currentData = options.currentData
    that.setData({
      zid: zid,
      back_orser: back_orser,
      key_words: key_words,
      j_id: j_id,
      ifpay: ifpay,
      money: money,
      currentData: currentData
    })
    if (options.id) {
      var id = options.id
      that.setData({
        id: id
      })
    }
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/user.detail',
      data: {
        j_id: j_id
      },
      success: function (res) {
        console.log(res)
        that.setData({
          usedetail: res.data
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    if (options.txt) {
      this.setData({
        txt: options.txt,
      })
    }
  },
  pingjia(e) {
    var that = this
    var keywords = e.currentTarget.dataset.keywords
    var zid = e.currentTarget.dataset.zid
    var orders = e.currentTarget.dataset.orders
    var j_id = e.currentTarget.dataset.j_id;
    var title = e.currentTarget.dataset.title;
    var did = e.currentTarget.dataset.did;
    wx.navigateTo({
      url: '/hyb_yl/lvtongserver/pages/pingjia/pingjia?keywords=' + keywords + '&zid=' + zid + '&orders=' + orders + '&j_id=' + j_id + '&title='+title + '&did='+did,
    })
  },
  quitclick() {
    this.setData({
      popshow: false,
      applyFor: false
    })
  },
  allowbtn() {
    this.setData({
      popshow: false,
      applyFor: false,
      txt: ""
    })
  },
  onHide: function () {
    clearInterval(time1)
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showNavigationBarLoading();
    this.getUserchar()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getUptotime()
    wx.showNavigationBarLoading();
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(time1)
    var that = this
    var currentData = that.data.currentData
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    that.setData({
      currentData: currentData
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //查询当前用户与医生的图文记录
  getUserchar() {
    var that = this
    var back_orser = that.data.back_orser
    app.util.request({
      'url':"entry/wxapp/green.order_message",
      data:{
        back_orser:back_orser,
        openid:wx.getStorageSync('openid'),
      },
      success:function(res){
        that.setData({
          list: res.data,
        })
      }
    })
    
  },
  uploadimg: function () {
    let that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.upload_picture_list;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              console.log(res)
              data_arr.push(res.data)
              that.setData({
                upload_picture_list: data_arr,
                typedate: 1
              })
            }
          })
        }
      }
    })
  },
  clearImg: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.ind;
    console.log(index);
    var nowList = []; //新数据
    var imglis = that.data.upload_picture_list; //原数据
    imglis.splice(index, 1);
    that.setData({
      upload_picture_list: imglis
    })
  },
  submitformzw(e) {
    var that = this;
    var val = e.detail.value.zhuiwen;
    var upload_picture_list = that.data.upload_picture_list
    var zid = that.data.zid
    var typedate = that.data.typedate
    var back_orser = that.data.back_orser
    var addnum = parseInt(that.data.addnum)
    if (that.data.id) {
      var id = that.data.id
    }
    var arr = {
      'text': val,
      'typedate': typedate,
      'upload_picture_list': upload_picture_list,
    }
    app.util.request({
      'url': 'entry/wxapp/green.adduserzhuiwen',
      data: {
        arr: arr,
        back_orser: back_orser,
      },
      success: function (res) {
        console.log(res)
        that.getUserchar()
        that.setData({
          shouwtab: false
        })
      }
    })
  },
  //取消订单
  cancel(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/green.order_change",
      data: {
        id: id,
        ifpay:'8'
      },
      success: function (res) {
        wx.showModal({
          title: '提示',
          content:'取消成功',
          showCancel:false,
          success:function(con){
            if(con.confirm)
            {
              wx.navigateBack({
                complete: (res) => {},
              })
            }
          }
        })
      }
    })
  },
  //去退款
  refund(e) {
    var that = this
    var id = e.currentTarget.dataset.id;
    app.util.request({
      'url': "entry/wxapp/green.order_change",
      data: {
        id: id,
        ifpay:'5'
      },
      success: function (res) {
        wx.showModal({
          title: '提示',
          content:"申请成功",
          showCancel:false,
          success:function(con)
          {
            if(con.confirm)
            {
              wx.navigateBack({
                complete: (res) => {},
              })
            }
          }
        })
      }
    })
  },
  buttonclick: function (e) {
    var that = this
    var back_orser = that.data.back_orser
    var key_words = that.data.key_words
    var open = e.currentTarget.dataset.open
    var id = that.data.id
    var ifpay = that.data.ifpay
    
    if (ifpay == '3') {
      var itemList = ['删除订单', msg]
    } else {
      var itemList = ['删除订单']

    }
    wx: wx.showActionSheet({
      itemList: itemList,
      itemColor: '',
      success: function (res) {
          if (!res.cancel) {
            wx.showModal({
              title: '是否删除订单？',
              content: '删除后不可恢复',
              success: (result) => {
                if (result.confirm) {
                  app.util.request({
                    'url': 'entry/wxapp/green.del_order',
                    data: {
                      back_orser: back_orser,
                      id:id
                    },
                    success: function (res) {
                      console.log(res)
                      wx.showToast({
                        title: '删除成功',
                        icon: 'none',
                        success: (res) => {
                          setTimeout(function () {
                            wx.navigateBack({
                              detail: 1
                            })
                          }, 2000)
                        },
                      })
                    }

                  })

                }
              },
            })
          }
        

      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  tabinputpay(e) {
    console.log(e)
    var that = this
    var back_orser = that.data.back_orser
    var money = that.data.money
    var key_words = that.data.key_words
    var zid = that.data.zid
    console.log(back_orser, money, key_words, zid)
    app.util.request({
      'url': 'entry/wxapp/green.payorder',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        z_tw_money: money,
        orders: back_orser,
        key_words: key_words
      },
      success: function (res) {
        console.log(res);
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
          },
          'complete': function (res) {
            clearInterval(time1)
            if (res.errMsg == 'requestPayment:ok') {
              wx.showModal({
                title: '提示',
                content: '支付成功'
              });
              that.getOrderdetail()
            }
            return;
          }
        })
      }
    })
  },
  tuikuan(e) {
    console.log(e)
    var that = this
    var back_orser = that.data.back_orser
    var key_words = that.data.key_words
    var money = that.data.money
    if (money == '0.00') {
      var title = "是否取消问诊"
    } else {
      var title = "是否申请退款"
    }
    wx.showModal({
      title: title,
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/user.tuikuan',
            data: {
              back_orser: back_orser,
              money: money,
              key_words: key_words
            },
            success: function (res) {
              if (money == '0.00') {
                var title2 = "取消成功"
              } else {
                var title2 = "申请成功，请耐心等待退款"
              }
              wx.showToast({
                title: title2,
                icon: 'none',
                success: function (res) {
                  console.log(res)
                  setTimeout(function () {
                    wx.navigateBack({
                      detail: 1
                    })
                  }, 1500)
                }
              })
            }
          })
        }
      }
    })
  },
  tabtwoinput() {
    var that = this
    var title = that.data.info.title;
    var keywords = that.data.info.key_words;
    wx.reLaunch({
      url: '/hyb_yl/czhuanjiasubpages/pages/longsever/index?ser_key='+keywords+'&name='+title,
    })
  },
  getOrderdetail() {
    var that = this
    var orders = that.data.orders
    var key_words = that.data.key_words
    app.util.request({
      'url':"entry/wxapp/green.order_detail",
      data:{
        id:that.data.id
      },
      success:function(res){
        that.setData({
          info:res.data 
        })
      }
    })
  },
  getUptotime() {
    var that = this
    wx.showNavigationBarLoading();
    time1 = setInterval(function () {
      that.getOrderdetail()
      that.getUserchar()
    }, 1000)
    wx.showNavigationBarLoading();
  },
})